﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Noea.TogSim.Model.Train;
using Noea.TogSim.Model.Tracks;
using Noea.TogSim.Model.Interfaces;
using System.Windows.Media;
using System.Windows;

namespace Prototype
{
    class GraphicsPanelViewModel
    {
        public TrainModel CurrentTrainModel { get; set; }
        public List<ITrack> Tracks { get; set; }

        public PathGeometry PathModel { get; set; }
        public int SegmentCount 
        {
            get { return PathModel.Figures.Count; }
        }

        public GraphicsPanelViewModel( TrainModel model )
        {
            this.CurrentTrainModel = model;
            this.PathModel = new PathGeometry();

            //PathModel.Figures.Add(
            //    new PathFigure(
            //        new Point(25,25), 
            //        new List<PathSegment>() 
            //        { 
            //            new LineSegment(new Point(0,0), true), 
            //            new LineSegment(new Point(10,10), true) 
            //        }, true));

            TraverseTracks();
        }

        public void TraverseTracks()
        {
            Tracks = new List<ITrack>();
            GetTracks(CurrentTrainModel.StartTrack);
        }

        public void GetTracks( ITrack track )
        {
            if (!Tracks.Contains(track))
            {
                Tracks.Add(track);
                DrawModel(track);
                GetTracks(track.Next);
            }
        }

        Random rand = new Random();

        private void DrawModel( ITrack track )
        {
            PathFigure lastFigure = PathModel.Figures.LastOrDefault();
            Point point = new Point(100, 100);

            if (lastFigure != null)
                point = lastFigure.StartPoint;

            Point endPoint = new Point(point.X + track.Length, point.Y + track.Length);

            List<PathSegment> segments = new List<PathSegment>();

            PathSegment startSegment = new LineSegment(point, true);
            PathSegment endSegment = new LineSegment(endPoint, true);

            segments.Add(startSegment);
            segments.Add(endSegment);

            PathFigure newTrack = new PathFigure(endPoint, segments, true);
            PathModel.Figures.Add(newTrack);
        }
    }
}
